package com.warm.pump.executor.batch;

import com.warm.pump.frame.util.SpringBeanUtil;
import com.warm.pump.module.skd.bean.po.gen.ScheduleInstanceDtl;
import com.warm.pump.module.skd.bean.po.gen.ScheduleLog;
import com.warm.pump.module.skd.service.IScheduleLogService;
import com.warm.pump.module.skd.service.impl.ScheduleLogServiceImpl;
import com.warm.pump.module.work.bean.po.gen.Work;
import com.warm.pump.module.work.bean.po.gen.WorkItem;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.batch.core.annotation.AfterJob;
import org.springframework.batch.core.annotation.BeforeJob;

import java.util.Date;

/**
 * Created by guozj on 2018/1/22.
 */
public class CursorJobListener implements JobExecutionListener {
    private LogMonitoringNotifier monitoringNotifier = new LogMonitoringNotifier();
    private ScheduleInstanceDtl instanceDtl;
    private WorkItem itemDtl;
    private Work work;
    private IScheduleLogService logService = SpringBeanUtil.getBean(ScheduleLogServiceImpl.class);
    private ScheduleLog scheduleLog;

    @Override
    @BeforeJob
    public void beforeJob(JobExecution jobExecution) {
        new Thread(() -> {
            scheduleLog = new ScheduleLog();
            scheduleLog.setInstanceId(instanceDtl.getInstanceId());
            scheduleLog.setInstanceDtlId(instanceDtl.getId());
            scheduleLog.setWorkId(instanceDtl.getWorkId());
            scheduleLog.setWorkName(instanceDtl.getWorkName());
            scheduleLog.setWorkLevelNum(work.getLevelNum());
            scheduleLog.setWorkItemId(itemDtl.getId());
            scheduleLog.setWorkItemName(itemDtl.getItemName());
            scheduleLog.setBatchJobId(jobExecution.getJobId());
            scheduleLog.setBatchJobInstId(jobExecution.getJobInstance().getId());
            scheduleLog.setUpdateTime(new Date());
            scheduleLog.setStatus(1);
            scheduleLog.setRemark("执行中");
            scheduleLog.setStartTime(new Date());
            scheduleLog.setDealCnt(0);
            logService.save(scheduleLog);
        }).start();
    }

    @Override
    @AfterJob
    public void afterJob(JobExecution jobExecution) {
        new Thread(() -> {
            //若log未生成，sleep 2秒
            if (null == scheduleLog || null == scheduleLog.getId()) {
                try {
                    Thread.sleep(2000l);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            if (null != scheduleLog && null != scheduleLog.getId()) {
                ScheduleLog updateLog = new ScheduleLog();
                updateLog.setId(scheduleLog.getId());
                updateLog.setEndTime(new Date());
                updateLog.setRemark("执行信息:" + jobExecution.toString());
                if (jobExecution.getStatus() == BatchStatus.FAILED) {
                    // Notify when job fails
                    String notify = monitoringNotifier.notify(jobExecution);
                    updateLog.setStatus(2);
                    updateLog.setError(notify);
                } else {
                    updateLog.setStatus(3);
                }
                updateLog.setUpdateTime(new Date());
                logService.update(updateLog);
            }
        }).start();
    }

    public void setInstanceDtl(ScheduleInstanceDtl instanceDtl) {
        this.instanceDtl = instanceDtl;
    }

    public void setItemDtl(WorkItem itemDtl) {
        this.itemDtl = itemDtl;
    }

    public void setWorkItem(Work work) {
        this.work = work;
    }
}
